<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input style="width: 130px;" v-model="queryParams.contractNo" placeholder="请输入合同编号" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="租客姓名" prop="leaseName">
        <el-input style="width: 130px;" v-model="queryParams.leaseName" placeholder="请输入租客姓名" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="租客手机号" prop="leasePhone">
        <el-input style="width: 130px;" v-model="queryParams.leasePhone" placeholder="请输入租客手机号" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="租客身份证号" prop="leaseIdno">
        <el-input v-model="queryParams.leaseIdno" placeholder="请输入租客身份证号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="合同状态" prop="contractState">
        <el-select v-model="queryParams.contractState" placeholder="请选择合同状态" clearable style="width: 130px;">
          <el-option v-for="dict in le_contract_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['lease:leLeaseContract:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['lease:leLeaseContract:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lease:leLeaseContract:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['lease:leLeaseContract:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leLeaseContractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractNo" width="150" />
      <el-table-column label="租客姓名" align="center" prop="leaseName" width="120"/>
      <el-table-column label="租客手机号" align="center" prop="leasePhone" width="150"/>
      <el-table-column label="租客身份证号" align="center" prop="leaseIdno" width="170"/>
      <el-table-column label="开始时间" align="center" prop="startDt" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDt" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="contractState"  width="90">
        <template #default="scope">
          <dict-tag :options="le_contract_state" :value="scope.row.contractState" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
            v-hasPermi="['lease:leLeaseContract:query']">详细</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lease:leLeaseContract:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lease:leLeaseContract:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 合同信息详细 -->
    <el-dialog title="租房合同信息详细" v-model="detail" width="900px" append-to-body>
      <el-form :model="form" label-width="120px">
        <el-row>
          <!-- <el-col :span="8">
            <el-form-item label="租房编号：">{{ form.leaseHouseId }}</el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="合同编号：">{{ form.contractNo }}</el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="用户编号：">{{ form.userId }}</el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="租客姓名：">{{ form.leaseName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租客手机号：">{{ form.leasePhone }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租客身份证号：">{{ form.leaseIdno }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房东姓名：">{{ form.landlordName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房东手机号：">{{ form.landlordPhone }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房东身份证号：">{{ form.landlordIdno }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="付款方式："> <template #default="scope">
                <dict-tag :options="le_pay_type" :value="form.payType" />
              </template></el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同开始时间：">
              <template #default="scope">
                <span>{{ parseTime(form.startDt, '{y}-{m}-{d}') }}</span>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同结束时间："> <template #default="scope">
                <span>{{ parseTime(form.endDt, '{y}-{m}-{d}') }}</span>
              </template></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="合同附件：">
              <template v-for="item in form.contractAddrs">
                &nbsp;&nbsp;<image-preview :src="item" :width="80" :height="80" />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detail = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改租房合同信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="leLeaseContractRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <!-- <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="租房编号" prop="leaseHouseId">
          <el-input v-model="form.leaseHouseId" placeholder="请输入租房编号" />
        </el-form-item> -->
        <el-form-item label="租客姓名" prop="leaseName">
          <el-input v-model="form.leaseName" placeholder="请输入租客姓名" />
        </el-form-item>
        <el-form-item label="租客手机号" prop="leasePhone">
          <el-input v-model="form.leasePhone" placeholder="请输入租客手机号" />
        </el-form-item>
        <el-form-item label="租客身份证号" prop="leaseIdno">
          <el-input v-model="form.leaseIdno" placeholder="请输入租客身份证号" />
        </el-form-item>
        <el-form-item label="房东姓名" prop="landlordName">
          <el-input v-model="form.landlordName" placeholder="请输入房东姓名" />
        </el-form-item>
        <el-form-item label="房东手机号" prop="landlordPhone">
          <el-input v-model="form.landlordPhone" placeholder="请输入房东手机号" />
        </el-form-item>
        <el-form-item label="房东身份证号" prop="landlordIdno">
          <el-input v-model="form.landlordIdno" placeholder="请输入房东身份证号" />
        </el-form-item>
        <el-form-item label="合同开始时间" prop="startDt">
          <el-date-picker clearable v-model="form.startDt" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择合同开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束时间" prop="endDt">
          <el-date-picker clearable v-model="form.endDt" type="date" value-format="YYYY-MM-DD" placeholder="请选择合同结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同附件" prop="contractAddr">
          <image-upload v-model="form.contractAddr" />
        </el-form-item>
        <el-form-item label="付款方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择付款方式">
            <el-option v-for="dict in le_pay_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同状态" prop="contractState">
          <el-select v-model="form.contractState" placeholder="请选择合同状态">
            <el-option v-for="dict in le_contract_state" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="LeLeaseContract">
import { listLeLeaseContract, getLeLeaseContract, delLeLeaseContract, addLeLeaseContract, updateLeLeaseContract } from "@/api/lease/leLeaseContract";

const { proxy } = getCurrentInstance();
const { le_pay_type, le_contract_state } = proxy.useDict('le_pay_type', 'le_contract_state');

const leLeaseContractList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const detail = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    contractNo: null,
    userId: null,
    leaseHouseId: null,
    leaseName: null,
    leasePhone: null,
    leaseIdno: null,
    landlordName: null,
    landlordPhone: null,
    landlordIdno: null,
    startDt: null,
    endDt: null,
    contractAddr: null,
    payType: null,
    contractState: null,
    contractAddr: null
  },
  rules: {
    contractNo: [
      { required: true, message: "合同编号不能为空", trigger: "blur" }
    ],
  //  leaseHouseId: [
    //  { required: true, message: "租房编号不能为空", trigger: "blur" }
    //],
    leaseName: [
      { required: true, message: "租客姓名不能为空", trigger: "blur" }
    ],
    leaseIdno: [
      { required: true, message: "租客身份证号不能为空", trigger: "blur" }
    ],
    contractState: [
      { required: true, message: "合同状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询租房合同信息列表 */
function getList() {
  loading.value = true;
  listLeLeaseContract(queryParams.value).then(response => {
    leLeaseContractList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  detail.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    contractId: null,
    contractNo: null,
    userId: null,
    leaseHouseId: null,
    leaseName: null,
    leasePhone: null,
    leaseIdno: null,
    landlordName: null,
    landlordPhone: null,
    landlordIdno: null,
    startDt: null,
    endDt: null,
    contractAddr: null,
    payType: null,
    contractState: null,
    updateBy: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("leLeaseContractRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.contractId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加租房合同信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _contractId = row.contractId || ids.value
  getLeLeaseContract(_contractId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改租房合同信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["leLeaseContractRef"].validate(valid => {
    if (valid) {
      if (form.value.contractId != null) {
        updateLeLeaseContract(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLeLeaseContract(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 详细按钮操作 */
function handleView(row) {
  detail.value = true;
  form.value = row;
  form.value.contractAddrs = form.value.contractAddr.split(",");
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _contractIds = row.contractId || ids.value;
  proxy.$modal.confirm('是否确认删除租房合同信息编号为"' + _contractIds + '"的数据项？').then(function () {
    return delLeLeaseContract(_contractIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('lease/leLeaseContract/export', {
    ...queryParams.value
  }, `leLeaseContract_${new Date().getTime()}.xlsx`)
}

getList();
</script>
